﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;

namespace Evan.Model
{
    /// <summary>
    /// 作者 : 谭远求
    /// 时间 : 2017年4月26日11:49:37
    /// 说明 : 动态Model , 用于返回类型的返回值设置 , 以及Model的赋值操作
    /// </summary>
    public class DynamicModel : DynamicObject
    {
        public System.Collections.Generic.Dictionary<string, object> _Map_In_Dictionary = new Dictionary<string, object>();


        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            if (_Map_In_Dictionary == null)
            {
                _Map_In_Dictionary = new System.Collections.Generic.Dictionary<string, object>();
            }

            string name = binder.Name;
            if (name == null)
            {
                //throw new ArgumentException("name");  
                return false;
            }

            value = Convert.IsDBNull(value) ? "" : value; //如果数据库返回是DBNull , 则返回空

            if (_Map_In_Dictionary.ContainsKey(name))
            {
                _Map_In_Dictionary[name] = value;
            }
            else
            {
                _Map_In_Dictionary.Add(name, value);
            }
            return true;

        }

        public override bool TryGetMember(System.Dynamic.GetMemberBinder binder, out object result)
        {
            if (_Map_In_Dictionary != null)
            {
                string name = binder.Name;
                object value;


                if (!_Map_In_Dictionary.ContainsKey(name))
                {
                    result = "";
                    return true;
                }


                if (_Map_In_Dictionary.TryGetValue(name, out value))
                {
                    result = value;
                    return true;
                }
            }
            return base.TryGetMember(binder, out result);
        }

        public override bool TryInvokeMember(System.Dynamic.InvokeMemberBinder binder, object[] args, out object result)
        {
            if (binder.Name == "Set" && binder.CallInfo.ArgumentCount == 2)
            {
                string name = args[0] as string;
                if (name == null)
                {
                    result = "";
                    return false;
                }
                if (_Map_In_Dictionary == null)
                {
                    _Map_In_Dictionary = new System.Collections.Generic.Dictionary<string, object>();
                }
                object value = args[1];

                value = Convert.IsDBNull(value) ? "" : value; //如果数据库返回是DBNull , 则返回空

                if (_Map_In_Dictionary.ContainsKey(name))
                {
                    _Map_In_Dictionary[name] = value;
                }
                else
                {
                    _Map_In_Dictionary.Add(name, value);
                }

                result = value;
                return true;

            }
            if (binder.Name == "Get" && binder.CallInfo.ArgumentCount == 1)
            {
                string name = args[0] as string;
                if (_Map_In_Dictionary.ContainsKey(name))
                {
                    result = _Map_In_Dictionary[name];
                }
                else
                {
                    result = "";
                }
                return true;
            }
            if (binder.Name == "Get" && binder.CallInfo.ArgumentCount == 2)
            {
                string name = args[0] as string;
                if (_Map_In_Dictionary.ContainsKey(name))
                {
                    result = _Map_In_Dictionary[name];
                }
                else
                {
                    result = args[1] as string;
                }
                return true;
            }
            if (binder.Name == "ToJson" && binder.CallInfo.ArgumentCount == 0)
            {
                result = Newtonsoft.Json.JsonConvert.SerializeObject(_Map_In_Dictionary, Newtonsoft.Json.Formatting.Indented);
                return true;
            }
            if (binder.Name == "HasKey" && binder.CallInfo.ArgumentCount == 1)
            {
                string name = args[0] as string;
                result = _Map_In_Dictionary.ContainsKey(name);
                return true;
            }
            return base.TryInvokeMember(binder, args, out result);
        }


    }
}
